/*
	系统自动取消超过DELAY时间的订单
*/

var TIMEOUT = (10 * 1000);//10秒循环
var DELAY = (60 * 60 * 1000);//一小时老订单自动取消

var clearTimeOutRequest = function () {
	var time = new Date();
	var delay = new Date(time - DELAY);
	var req = UserRequests.findOne({
		time: {$lt: delay},
		state: 'new'
	});
	if (req) {
		Messages.insert({userid: req.userId, msg: '自动取消过期订单', read: false});
	    UserRequests.update({
	      	'_id': req._id
	    }, {
			$set: {
				'state': 'cancel'
			}
	    });

	    Meteor.call('setRequest', req.userId, false);
	}
};

Meteor.setInterval(clearTimeOutRequest, TIMEOUT)